package com.gzhryc.shared_device.oem.code.agent.services;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.agent.dao.AgentLevelFuncModelDao;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentLevelFuncModel;
import com.gzhryc.shared_device.oem.code.agent.dao.enums.AgentLevelFuncModelEnum;
import com.gzhryc.shared_device.oem.code.agent.dao.models.FuncModelCount;
import com.gzhryc.shared_device.oem.code.config.dao.db.FuncModel;
import com.gzhryc.shared_device.oem.code.config.dao.enums.FuncModelEnum;
import com.gzhryc.shared_device.oem.code.config.services.FuncModelService;
import com.gzhryc.shared_device.oem.code.user.ERoleType;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class AgentLevelFuncModelService {

    static Logger log = Logger.getLogger(AgentLevelFuncModelService.class);

    public static AgentLevelFuncModelService self() {
        return MultiDBTools.getService(AgentLevelFuncModelService.class, true);
    }

    public static AgentLevelFuncModelService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, AgentLevelFuncModelService.class, true);
    }

    AgentLevelFuncModelDao dao;

    public AgentLevelFuncModelService(String jdbcKey) {
        this.dao = new AgentLevelFuncModelDao(jdbcKey);
    }

    public List<AgentLevelFuncModel> findJoinByLevel(Integer level) {
        if (level != null) {
            Conditions conditions = new Conditions(AgentLevelFuncModel.class);
            conditions.and().is("level", level);
            try {
                return dao.findJoinByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public AgentLevelFuncModel getJoinByKeys(Integer level, Long funcModelId) {
        if (level != null && funcModelId != null) {
            Conditions conditions = new Conditions(AgentLevelFuncModel.class);
            conditions.and().is("level", level);
            conditions.and().is("funcModelId", funcModelId);
            try {
                return dao.getJoin(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public List<FuncModelCount> findCountByFuncModelIds(List<Long> funcModelIds){
        Conditions conditions = new Conditions(AgentLevelFuncModel.class);
        conditions.and().in("funcModelId",funcModelIds);
        try {
            return dao.countGroupFuncModelId(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public boolean add(AgentLevelFuncModel agentLevelFuncModel) throws LogicException {
        FuncModel funcModel = FuncModelService.self().getById(agentLevelFuncModel.getFuncModelId());
        if(funcModel == null || !ERoleType.Agent.index().equals(funcModel.getType())){
            throw new LogicException("未找到功能模块或非代理功能模块");
        }

        if(FuncModelEnum.State.Default.index().equals(funcModel.getState())){
            agentLevelFuncModel.setState(AgentLevelFuncModelEnum.State.Close.index());
        }
        agentLevelFuncModel.setCreateDate(new Date());
        try {
            return dao.insert(agentLevelFuncModel) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }

    public boolean updateState(Integer level, Long funcModelId,Integer state)throws LogicException {
        if (level != null && funcModelId != null) {
            Conditions conditions = new Conditions(AgentLevelFuncModel.class);
            conditions.and().is("level", level);
            conditions.and().is("funcModelId", funcModelId);

            AgentLevelFuncModel updateEntity = new AgentLevelFuncModel();
            updateEntity.setState(state);

            try {
                return dao.updateNotNull(updateEntity, conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteByKeys(Integer level, Long funcModelId) throws LogicException {
        if (level != null && funcModelId != null) {
            Conditions conditions = new Conditions(AgentLevelFuncModel.class);
            conditions.and().is("level", level);
            conditions.and().is("funcModelId", funcModelId);

            try {
                return dao.deleteByCondition(conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteByFuncModelId(Long funcModelId) {
        if (funcModelId != null) {
            Conditions conditions = new Conditions(AgentLevelFuncModel.class);
            conditions.and().is("funcModelId", funcModelId);
            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteByLevel(Integer level) {
        if (level != null) {
            Conditions conditions = new Conditions(AgentLevelFuncModel.class);
            conditions.and().is("level", level);
            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

}
